package com.xumt.hwHMSdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.xumt.hwHMSdemo.cannectSb.HttpRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PaidActivity extends AppCompatActivity {
    private SimpleAdapter paidadapter = null;
    private String paidlistresult;
    private String shopname;
    private List<Map<String, Object>> paidlist = new ArrayList<Map<String, Object>>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paid);
        paidadapter = new PaidActivity.MySimpleAdapter(this, getData(), R.layout.paidlist, new String[]{"name","shopname", "price", "description","img"},
                new int[]{R.id.paidfood, R.id.paidshop,R.id.paidprice, R.id.paiddescription,R.id.paidimg});
        ListView listView = (ListView) findViewById(R.id.paidlist);
        listView.setAdapter(paidadapter);
        TextView textView=(TextView)findViewById(R.id.paidaddress);
        textView.setText(OrderActivity.address);
        Button button1 =(Button)findViewById(R.id.allcheck);//全部确认
        button1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                checkall(OrderActivity.clickoderid);
            }
        });
    }
    //通过state查找订单
    private void findstate(int id) {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    paidlistresult = request.sendGet("http://169.254.105.90:8081/Order2/getrelaid","relaid="+ id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    //确认收货
    private void checkall(int id) {
        new Thread() {
            public void run() {
                try {
                    HttpRequest request = new HttpRequest();
                    request.sendGet("http://169.254.105.90:8081/Order/modify4","orderid="+ id);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    //获取数据
    private List<Map<String, Object>> getData() {
        try {
            findstate(OrderActivity.clickoderid);
            Thread.sleep(500);
            JSONArray jsonArray = new JSONArray(paidlistresult);
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("name", "商品名：" + jsonObject.getString("name"));
                map.put("price", "价格：" + jsonObject.getString("price"));
                map.put("description", "简介：" + jsonObject.getString("description"));
                if(jsonObject.getInt("shopid")==1)
                {
                    shopname="zmhshop";
                }
                if(jsonObject.getInt("shopid")==2)
                {
                    shopname="开心宠物商店";
                }
                map.put("shopname", "店铺：" + shopname);
                Context ctx=getBaseContext();
                int resId = getResources().getIdentifier(jsonObject.getString("image"), "drawable" ,ctx.getPackageName());
                map.put("img",resId);
                paidlist.add(map);
            }
        } catch (JSONException | InterruptedException e) {
            e.printStackTrace();
        }
        return paidlist;
    }
    private class MySimpleAdapter extends SimpleAdapter {

        public MySimpleAdapter(Context context,
                               List<? extends Map<String, ?>> data, int resource,
                               String[] from, int[] to) {
            super(context, data, resource, from, to);
            // TODO Auto-generated constructor stub
        }


    }
}